<?php
$catid = empty($_GET['catid']) ? 0 : intval($_GET['catid']);
$id = empty($_GET['id']) ? 0 : intval($_GET['id']);

if($id > 0){
	$oGoods = new Goods();
	$goodsinfo = $oGoods->get_row($id, '*');
	$album_list = $oGoods->get_album_all($goodsinfo['id'], '*');
	
	$goodsinfo['addtime'] = ftime($goodsinfo['addtime'], true);
	$goodsinfo['updatetime'] = ftime($goodsinfo['updatetime'], true);


	// 隶属品牌信息
	if(!empty($goodsinfo['brand'])){
		$oBrand = new Brand();
		$goodsinfo['brandrow'] = $oBrand->get_row($goodsinfo['brand'], '`id`,`name`,`logo`');
		$goodsinfo['brand_json'] = json_encode($goodsinfo['brandrow']);
	}

	// 促销信息
	if(!empty($goodsinfo['salesinfo'])){
		$oNews = new News();
		$goodsinfo['salesinforow'] = $oNews->get_row($goodsinfo['salesinfo'], '`id`,`title`');
		$goodsinfo['salesinfo_json'] = json_encode($goodsinfo['salesinforow']);
	}

	// 关联新闻信息
	if(!empty($goodsinfo['related_news'])){
		$ids = explode(',', trim($goodsinfo['related_news'], ', '));
		if(!isset($oNews))$oNews = new News();
		$goodsinfo['related_news_list'] = array();
		foreach($ids as $newsid){
			$goodsinfo['related_news_list'][$newsid] = $oNews->get_row($newsid, '`id`,`title`');
		}
		$goodsinfo['related_news_json'] = json_encode($goodsinfo['related_news_list']);
	}

	// 配件信息
	if(!empty($goodsinfo['fittings'])){
		$ids = unserialize($goodsinfo['fittings']);
		$oFittings = new Fittings();
		$goodsinfo['fittings_list'] = array();
		foreach($ids as $fittingsid => $num){
			$goodsinfo['fittings_list'][$fittingsid] = $oFittings->get_row($fittingsid, '`id`,`name`');
			$goodsinfo['fittings_list'][$fittingsid]['num'] = $num;
		}
		$goodsinfo['fittings_json'] = json_encode($goodsinfo['fittings_list']);
	}
	
	// 人气组合信息
	if(!empty($goodsinfo['combine_goods'])){
		$ids = explode(',', trim($goodsinfo['combine_goods'], ', '));
		$goodsinfo['combine_goods_list'] = array();
		foreach($ids as $goodsid){
			$goodsinfo['combine_goods_list'][$goodsid] = $oGoods->get_row($goodsid, '`id`,`name`,`cover`');
		}
		$goodsinfo['combine_goods_json'] = json_encode($goodsinfo['combine_goods_list']);
	}

}else{
	$goodsinfo = array(
		'id'		=> $id,
		'catid'		=> $catid,
		'weight'	=> 0,
		'state'		=> 1,
		'rank'		=> 1000000,
		'recommend'	=> 'normal',
		'labeltype'	=> '',
		'show'		=> 1,
		'addtime'	=> ftime(TIME)
	);
	$album_list = array(/*array('i' => 1, 'base' => '', 'ext' => '', 'alt' => '', 'rank' => 0, 'info' => '')*/);
	// 配件
	$oFittings = new Fittings();
	$goodsinfo['fittings_list'] = array();
	$goodsinfo['fittings_list'][1] = $oFittings->get_row(1, '`id`,`name`');
	$goodsinfo['fittings_list'][1]['num'] = 1;
	$goodsinfo['fittings_json'] = json_encode($goodsinfo['fittings_list']);
}

// 格式化重量
$goodsinfo['weightunit'] = empty($goodsinfo['weightunit']) ? '' : $goodsinfo['weightunit'];
$goodsinfo['weight'] = format_weight_out($goodsinfo['weight'], $goodsinfo['weightunit']);
$goodsinfo['weightunit_options'] = make_options(Statics::$goods_weight_unit_arr, false, $goodsinfo['weightunit']);
// 库存单位
$goodsinfo['quantityunit'] = empty($goodsinfo['quantityunit']) ? '' : $goodsinfo['quantityunit'];
$goodsinfo['quantityunit_options'] = make_options(Statics::$goods_quantity_unit_arr, false, $goodsinfo['quantityunit']);

if($goodsinfo['catid'] > 0){
	$categorys = cache_read('goods_categorys.php');
	$goodsinfo['category_name'] = $categorys['category_list'][$goodsinfo['catid']]['name'];
}else{
	$goodsinfo['category_name'] = $__l['root'];
}
$goodsinfo['recommend_radios'] = make_radio_group('recommend', Statics::$recommend_type_arr, $goodsinfo['recommend']);
$goodsinfo['labeltype_radios'] = make_radio_group('labeltype', Statics::$label_type_arr, $goodsinfo['labeltype']);

// 分类树
$oGoodscat = new Goodscat();
$goodsinfo['category_tree'] = $oGoodscat->get_category_tree_ul(); // 取分类树


// 相册
$album_count = 0;
if(!empty($album_list)){
	foreach($album_list['list'] as $key => $album){
		$album_count++;
		$album['i'] = $album_count;
		$album_list['list'][$key] = $album;
	}
	$goodsinfo['album_list'] = $album_list['list'];
}
$goodsinfo['album_count'] = $album_count;

$vars['info'] = $goodsinfo;
?>